home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 118_01.zip / FILE.BDS < prev    next >
Text File  |  1993-06-03  |  11KB  |  696 lines

  1. /* I/O routines and primitives for software tools
  2.  * source:  newfile.bds
  3.  * version: November 25, 1981
  4.  *
  5.  *        This file is the version of August 9, 1981
  6.  *        put in alphabetical order.
  7.  */
  8.  
  9. #include tools.h
  10.  
  11. /* make the calls to buffered BDS routines more visible */
  12.  
  13. #define BDS_fclose   _fclose
  14. #define BDS_fcreat   _fcreat
  15. #define BDS_fflush   _fflush
  16. #define BDS_fopen    _fopen
  17. #define BDS_getc     _getc
  18. #define BDS_gets     _gets
  19. #define BDS_gchar    _getchar
  20. #define BDS_fopen    _fopen
  21. #define BDS_putc     _putc
  22. #define BDS_pchar    _putchar
  23. #define BDS_rename   _rename
  24. #define BDS_unlink   _unlink
  25.  
  26. /*
  27. This file contains:
  28.  
  29.     _assign        _chkacc        _getunit
  30.     _open1
  31.  
  32.     close        creat
  33.     fcopy        flush
  34.     getc        getch        getch1
  35.     getlin
  36.     isatty
  37.     mkuniq
  38.     note
  39.     open
  40.     prompt
  41.     putc        putch        putch1
  42.     putdec        putint
  43.     putlin        putstr
  44.     readf        remove        rename
  45.     seek
  46.     writef
  47.     
  48. */
  49.  
  50. /*    _assign - open a file for STDIN, STDOUT, or ERROUT
  51.  *                redirect I/O to that file.
  52.  */
  53.  
  54. BOOL _assign(unit, filename, access)
  55. int  unit;
  56. char *filename;
  57. int access;
  58. {
  59.     BOOL _open1();
  60.  
  61.     if (unit < 0 || unit >= SYS_STD) {
  62.         sys_error("_assign\n");
  63.     }
  64.  
  65.     if (access == READ && unit != STDIN) {
  66.         return(NO);
  67.     }
  68.  
  69.     if (access == WRITE && unit == STDIN) {
  70.         return(NO);
  71.     }
  72.  
  73.     /* open the unit */
  74.     if (_open1(unit, filename, access) == NO) {
  75.         return(NO);
  76.     }
  77.  
  78.     /* comment out -----
  79.     printf("assign:  unit = %d name = %s\n",unit,filename);
  80.     ----- end comment out */
  81.  
  82.     return(YES);
  83. }
  84.  
  85. /*    _chkacc - check access parameter */
  86.  
  87. BOOL _chkacc(access)
  88. int access;
  89. {
  90.     if (access == READ || access == WRITE) {
  91.         return (YES);
  92.     }
  93.     if (access == READWRITE) {
  94.         error("READWRITE access not supported\n");
  95.     }
  96.     else if (access == APPEND) {
  97.         error("APPEND access not supported\n");
  98.     }
  99.     else {
  100.         printf("access = %d\n",access);
  101.         error("unknown file access type\n");
  102.     }
  103.     return(NO);
  104. }
  105.  
  106. /*    _getunit - return an available file unit number */
  107.  
  108. int _getunit()
  109. {
  110.  
  111.     int i, j;
  112.  
  113.     /* search sys_bufn [] for unit i */
  114.  
  115.     for (i = SYS_STD; i < MAXOFILES + MAXOFILES; i++) {
  116.         if (sys_spec [i] != ERR) {
  117.             continue;
  118.         }
  119.         for (j = 0; j < SYS_STD + MAXOFILES; j++) {
  120.             if (sys_bufn [j] == i) {
  121.                 /* buffer i in use */
  122.                 break;
  123.             }
  124.         }
  125.         if (j == SYS_STD + MAXOFILES) {
  126.             /* free buffer found */
  127.             return(i);
  128.         }
  129.     }
  130.     return(ERR);
  131. }
  132.  
  133. /*    _open1 - internal open routine */
  134.  
  135. BOOL _open1(unit, filename, access)
  136. int  unit;
  137. char *filename;
  138. int access;
  139. {
  140.  
  141.     FILEDES fd;
  142.     int  diskunit;
  143.     char *iobuf;
  144.  
  145.     if (unit < 0 || unit >= SYS_STD + MAXOFILES) {
  146.         sys_error("_open1:  bad unit\n");
  147.     }
  148.  
  149.     /* fill in access mode */
  150.     if (_chkacc(access) == NO) {
  151.         return(NO);
  152.     }
  153.     else {
  154.         sys_acc [unit] = access;
  155.     }
  156.  
  157.     /* check for special files */
  158.     if (equal (filename, TERMINAL)) {
  159.         sys_spec [unit] = SYS_TERM;
  160.         return(YES);
  161.     }
  162.     else if (equal (filename, PRINTER)) {
  163.         if (access == READ) {
  164.             return(NO);
  165.         }
  166.         else {
  167.             sys_spec [unit] = SYS_PRT;
  168.             return(YES);
  169.         }
  170.     }
  171.  
  172.     if (unit < SYS_STD) {
  173.         /* find a disk file slot */
  174.         if ((diskunit = _getunit()) == ERR) {
  175.             return(NO);
  176.         }
  177.     }
  178.     else {
  179.         diskunit = unit;
  180.     }
  181.  
  182.     iobuf = sys_bufs + ((diskunit - SYS_STD) * BUFSIZ);
  183.  
  184.     if (access == READ) {
  185.         fd = BDS_fopen(filename, iobuf);
  186.         if (fd == ERR) {
  187.             return(NO);
  188.         }
  189.         else {
  190.             /* open system tables */
  191.             sys_bufp [unit] = iobuf;
  192.             sys_bufn [unit] = diskunit;
  193.             return(YES);
  194.         }
  195.     }
  196.     if (access == WRITE) {
  197.         /* erase the file if it exists ! */
  198.         fd = BDS_fcreat (filename, iobuf);
  199.         if (fd == ERR) {
  200.             return(NO);
  201.         }
  202.         else {
  203.             /* open system tables */
  204.             sys_bufp [unit] = iobuf;
  205.             sys_bufn [unit] = diskunit;
  206.             return(YES);
  207.         }
  208.     }
  209.  
  210.     /* unknown access */
  211.     return(NO);
  212. }
  213.  
  214. /*    close - close unit  */
  215.  
  216. close (unit)
  217. int  unit;
  218. {
  219.  
  220.     if (unit < 0 || unit >= SYS_STD + MAXOFILES) {
  221.         return;
  222.     }
  223.     else if (sys_spec [unit] != ERR) {
  224.         if ( unit == STDIN ||
  225.              unit == STDOUT ||
  226.              unit == ERROUT
  227.            ) {
  228.             sys_spec [unit] = SYS_TERM;
  229.         }
  230.         else {
  231.             sys_spec [unit] = ERR;
  232.         }
  233.     }
  234.  
  235.     if (sys_bufp [unit] == ERR) {
  236.         return;
  237.     }
  238.  
  239.     /* write end of file mark and flush for output files */
  240.     if (sys_acc [unit] != READ) {
  241.         putch (CPMEOF, unit);
  242.         flush (unit);
  243.     }
  244.  
  245.     /* close the file */
  246.     BDS_fclose(sys_bufp [unit]);
  247.     sys_bufp [unit] = ERR;
  248.     sys_bufn [unit] = ERR;
  249. }
  250.  
  251. /*    create -  create a clean file
  252.  *                erase it if it exists.
  253.  */
  254.  
  255. int create (filename, access)
  256. char *filename;
  257. int access;
  258. {
  259.  
  260.     int unit;
  261.     BOOL _open1();
  262.  
  263.     if (access != WRITE) {
  264.         error("in create:  bad access mode\n");
  265.     }
  266.  
  267.     if ((unit = _getunit()) == ERR) {
  268.         return(ERR);
  269.     }
  270.  
  271.     if (_open1(unit, filename, WRITE) == NO) {
  272.         return(ERR);
  273.     }
  274.     else {
  275.         return(unit);
  276.     }
  277. }
  278.  
  279. /*    fcopy - copy file in to file out */
  280.  
  281. fcopy (in, out)
  282. int in, out;
  283. {
  284.     char line [MAXLINE];
  285.  
  286.     while (getlin (line, in) != EOF) {
  287.         putlin (line, out);
  288.     }
  289. }
  290.  
  291. /*    flush - flush file's output buffer */
  292.  
  293. flush (unit)
  294. int unit;
  295. {
  296.     if (unit < 0 || unit >= SYS_STD + MAXOFILES) {
  297.         error("flush:  bad unit\n");
  298.     }
  299.  
  300.     if (sys_bufp [unit] == ERR) {
  301.         return;
  302.     }
  303.  
  304.     /* flush buffer */
  305.     if (sys_acc [unit] != READ) {
  306.         BDS_fflush(sys_bufp [unit]);
  307.     }
  308. }
  309.  
  310. /*    getc - get char from STDIN */
  311.  
  312. int getc(c)
  313. char c;
  314. {
  315.     return(getch(STDIN));
  316. }
  317.  
  318. /*    getch - get a character from a file
  319.  *              return the character and set c to character.
  320.  *              Note:  this is a change from Software Tools
  321.  */
  322.  
  323. int getch (unit)
  324. int unit;
  325. {
  326.     int c;
  327.  
  328.     /* check unit once here */
  329.     if (unit < 0 || unit >= SYS_STD + MAXOFILES) {
  330.         error("in getch:  bad unit\n");
  331.     }
  332.  
  333.     /* ignore all carriage returns */
  334.     while ((c = getch1(unit)) == CR) {
  335.         ;
  336.     }
  337.     return(c);
  338. }
  339.  
  340. int getch1 (unit)
  341. int unit;
  342. {
  343.  
  344.     int c;
  345.  
  346.     if (sys_bufp [unit] != ERR) {
  347.         c = BDS_getc(sys_bufp [unit]);
  348.         if (c == CPMEOF) {
  349.             return(EOF);
  350.         }
  351.         else {
  352.             return(c);
  353.         }
  354.     }
  355.  
  356.     else if (sys_spec [unit] == SYS_TERM) {
  357.             c = BDS_gchar();
  358.             if (c == CPMEOF) {
  359.                 return(EOF);
  360.             }
  361.             else {
  362.                 return(c);
  363.             }
  364.     }
  365.     else {
  366.         error("getch:  unit not open\n");
  367.     }
  368. }
  369.  
  370. /*    getlin - get a line from a unit
  371.  *             return number of chars in the line.
  372.  */
  373.  
  374. int getlin (line, unit)
  375. char *line;
  376. int unit;
  377. {
  378.  
  379.     int i;
  380.     int c;
  381.  
  382.     if (unit < 0 || unit >= SYS_STD + MAXOFILES) {
  383.         error("getlin:  bad unit\n");
  384.     }
  385.  
  386.     if ( sys_bufp [unit] == ERR &&
  387.          sys_spec [unit] == SYS_TERM
  388.        ) {
  389.         /* allow delete char & line functions */
  390.         BDS_gets(line);
  391.         if (line [0] == (CPMEOF & 0xff)) {
  392.             return(EOF);
  393.         }
  394.         for (i = 0; line [i] != EOS; i++) {
  395.             ;
  396.         }
  397.         return (i);
  398.     }
  399.  
  400.     for (i = 0; i < MAXLINE - 1; i++) {
  401.         c = getch(unit);
  402.         if (c == EOF) {
  403.             line [i] = EOS;
  404.             return (EOF);
  405.         }
  406.         else if (c == NEWLINE) {
  407.             line [i] = c;
  408.             line [i + 1] = EOS;
  409.             return (i);
  410.         }
  411.         else {
  412.             line [i] = c;
  413.         }
  414.     }
  415.     line [MAXLINE-1] = EOS;
  416.     return (MAXLINE);
  417. }
  418.  
  419. /*    isatty - determine if file is a teletype/CRT device */
  420.  
  421. BOOL isatty (unit)
  422. int unit;
  423. {
  424.     int type;
  425.  
  426.     if (sys_bufp [unit] != ERR) {
  427.         /* disk file */
  428.         return(NO);
  429.     }
  430.     else if (sys_spec [unit] != ERR) {
  431.         /* special file */
  432.         return(YES);
  433.     }
  434.     else {
  435.         /* unit not open */
  436.         return(NO);
  437.     }
  438. }
  439.  
  440. /*    mkuniq - make a scratch file name based on 'seed' */
  441.  
  442. mkuniq (seed, name)
  443. char seed[], name[];
  444. {
  445.  
  446.     /* name is $$A.$$$ through $$Z.$$$ */
  447.  
  448.     scopy ("$$", 0, name, 0);
  449.     name [2] = seed + 'A';
  450.     scopy (".$$$", 0, name, 3);
  451. }
  452.  
  453. /*    note - determine current file position */
  454.  
  455. note (unit, offset)
  456. int unit;
  457. int offset[2];
  458. {
  459.     error("note() not implemented\n");
  460. }
  461.  
  462. /*    open - open a file */
  463.  
  464. int open (filename, access)
  465. char *filename;
  466. int access;
  467. {
  468.  
  469.     int unit;
  470.     BOOL _open1();
  471.  
  472.     /* get an available file number */
  473.     if ((unit = _getunit()) == ERR) {
  474.         return(ERR);
  475.     }
  476.  
  477.     /* open the file */
  478.     if (_open1(unit, filename, access) == NO) {
  479.         return(ERR);
  480.     }
  481.     else {
  482.         return(unit);
  483.     }
  484. }
  485.  
  486. /*    prompt - write to/read from teletype */
  487.  
  488. prompt (str, buf, unit)
  489. char *str, *buf;
  490. int  unit;
  491. {
  492.  
  493.     int i;
  494.  
  495.     if (isatty(unit) == YES) {
  496.         putlin (str, unit);
  497.         flush (unit);
  498.     }
  499.     getlin (buf, unit);
  500. }
  501.  
  502. /*    putc - put char c onto STDOUT
  503.  *             return c
  504.  */
  505.  
  506. int putc (c)
  507. char c;
  508. {
  509.     return (putch (c, STDOUT));
  510. }
  511.  
  512. /*    putch - put char 'c' to a file
  513.  *             return 'c'
  514.  */
  515.  
  516. int putch (c, unit)
  517. {
  518.     /* check unit once here */
  519.     if (unit < 0 || unit >= SYS_STD + MAXOFILES) {
  520.         error("in putch:  bad unit\n");
  521.     }
  522.  
  523.     /* add carriage returns before LF (NEWLINE) */
  524.     if (c == NEWLINE) {
  525.         putch1 (CR, unit);
  526.     }
  527.     return(putch1 (c, unit));
  528. }
  529.  
  530. int putch1 (c, unit)
  531. char c;
  532. int  unit;
  533. {
  534.  
  535.     if (sys_bufp [unit] != ERR) {
  536.         return(BDS_putc(c, sys_bufp [unit]));
  537.     }
  538.     else if (sys_spec [unit] == SYS_TERM) {
  539.         return(BDS_pchar(c));
  540.     }
  541.     else if (sys_spec [unit] == SYS_PRT) {
  542.         bdos(5,c);
  543.         return(c);
  544.     }
  545.     else {
  546.         error("in putch:  unit not open\n");
  547.     }
  548. }
  549.  
  550. /*    putdec - put decimal int n in field width >= w */
  551.  
  552. putdec(n,w)
  553. int n, w;
  554. {
  555.  
  556.     char chars [MAXCHARS];
  557.  
  558.     int i, nd;
  559.  
  560.     nd = itoc (n, chars, MAXCHARS);
  561.     for (i = nd; i < w; i++) {
  562.         putc (' ');
  563.     }
  564.     for (i = 0; i < nd; i++) {
  565.         putc (chars[i]);
  566.     }
  567. }
  568.  
  569. /*    putint - output int in specified field */
  570.  
  571. putint (n, w, unit)
  572. int n, w;
  573. int unit;
  574. {
  575.  
  576.     char chars [MAXCHARS];
  577.  
  578.     itoc (n, chars, MAXCHARS);
  579.     putstr (chars, w, unit);
  580. }
  581.  
  582. /*    putlin - output a line to a unit */
  583.  
  584. putlin (line, unit)
  585. char *line;
  586. int unit;
  587. {
  588.  
  589.     int i;
  590.  
  591.     for (i = 0; line [i] != EOS; i++) {
  592.         putch (line [i], unit);
  593.     }
  594. }
  595.  
  596. /*    putstr - output char string in specified field */
  597.  
  598. putstr (str, w, unit)
  599. char *str;
  600. int w;
  601. int unit;
  602. {
  603.     int i, len;
  604.  
  605.     len = length (str);
  606.  
  607.     /* output leading blanks blanks */
  608.     for (i = len; i < w; i++) {
  609.         putch (' ', unit);
  610.     }
  611.  
  612.     /* output string */
  613.     for (i = 0; i < len; i++) {
  614.         putch (str [i], unit);
  615.     }
  616.  
  617.     /* output trailing blanks */
  618.     for (i = (-w) - len; i > 0; i--) {
  619.         putch (' ', unit);
  620.     }
  621. }
  622.  
  623. /*    readf - read 'n' bytes/words/whatever from file fd */
  624.  
  625. readf (buf, n, unit)
  626. char *buf;
  627. int n;
  628. int unit;
  629. {
  630.  
  631.     int i;
  632.  
  633.     for (i = 0; i < n; i++) {
  634.         if ((buf [i] = getch(unit)) == EOF) {
  635.             buf[i] = EOS;
  636.             return (EOF);
  637.         }
  638.     }
  639.     buf[i] = EOS;
  640.     return (i);
  641. }
  642.  
  643. /*    remove - remove a file from the file system */
  644.  
  645. BOOL remove (filename)
  646. char *filename;
  647. {
  648.     return (BDS_unlink(filename));
  649. }
  650.  
  651. /*    rename - change old file name to new file name */
  652.  
  653. BOOL rename (old, new)
  654. char *old, *new;
  655. {
  656.     BDS_rename(old, new);
  657.     return(OK);
  658. }
  659.  
  660. /*    writef - write 'n' bytes/words/whatever to file */
  661.  
  662. writef (buf, n, unit)
  663. char *buf;
  664. int n;
  665. int unit;
  666. {
  667.  
  668.     int i;
  669.  
  670.     for (i = 0; i < n; i++) {
  671.         if (buf[i] == EOS) {
  672.             break;
  673.         }
  674.         putch (buf[i], unit);
  675.     }
  676.     return (i);
  677. }
  678.  
  679. e system */
  680.  
  681. BOOL remove (filename)
  682. char *filename;
  683. {
  684.     return (BDS_unlink(filename));
  685. }
  686.  
  687. /*    rename - change old file name to new file name */
  688.  
  689. BOOL rename (old, new)
  690. char *old, *new;
  691. {
  692.     BDS_rename(old, new);
  693.     return(OK);
  694. }
  695.  
  696. /*    writef - w